package com.xk.meeting.controller;

import com.xk.meeting.service.UserService;
import com.xk.meeting.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/modifyPassword")
public class ModifyPasswordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        String id = request.getParameter("id");
        String oldpass = request.getParameter("oldpass");
        String newpass = request.getParameter("newpass");
        UserService userService = new UserServiceImpl();
        String password = userService.getPasswordById(Integer.parseInt(id));

        if(password.equals(oldpass)){
            int count = userService.updatePassword(id,newpass);
            if(count > 0){
                request.setAttribute("msg","修改成功");
                request.getRequestDispatcher("update.jsp").forward(request,response);
            }else {
                response.sendRedirect("errorPage.jsp");
            }
        }else {
            request.setAttribute("msg","原密码错误");
            request.getRequestDispatcher("update.jsp").forward(request,response);
        }


    }
}
